// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <tests/expression/fuzzer/arg_types_generator_test_utils.h>
#include <pollux/functions/prestosql/fuzzer/DivideArgTypesGenerator.h>
#include <pollux/functions/prestosql/fuzzer/FloorAndRoundArgTypesGenerator.h>
#include <pollux/functions/prestosql/fuzzer/ModulusArgTypesGenerator.h>
#include <pollux/functions/prestosql/fuzzer/MultiplyArgTypesGenerator.h>
#include <pollux/functions/prestosql/fuzzer/PlusMinusArgTypesGenerator.h>
#include <pollux/functions/prestosql/fuzzer/TruncateArgTypesGenerator.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>

using namespace kumo::pollux;
using namespace kumo::pollux::fuzzer::test;
namespace {

class ArgTypesGeneratorTest : public functions::test::FunctionBaseTest {
 protected:
  // Returns the only signature with decimal return type for a given function
  // name.
  const exec::FunctionSignature& getOnlySignature(
      const std::string& functionName) {
    const auto signatures = getSignatures(functionName, "decimal");
    POLLUX_CHECK_EQ(signatures.size(), 1);
    return *signatures[0];
  }
};

TEST_F(ArgTypesGeneratorTest, plus) {
  const auto& signature = getOnlySignature("plus");
  const auto generator =
      std::make_shared<exec::test::PlusMinusArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 2));
  assertReturnType(generator, signature, DECIMAL(32, 6));
  assertReturnType(generator, signature, DECIMAL(38, 20));
  assertReturnType(generator, signature, DECIMAL(38, 38));
  assertReturnType(generator, signature, DECIMAL(38, 0));
  assertEmptyArgs(generator, signature, DECIMAL(18, 18));
}

TEST_F(ArgTypesGeneratorTest, minus) {
  const auto& signature = getOnlySignature("minus");
  const auto generator =
      std::make_shared<exec::test::PlusMinusArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 2));
  assertReturnType(generator, signature, DECIMAL(32, 6));
  assertReturnType(generator, signature, DECIMAL(38, 20));
  assertReturnType(generator, signature, DECIMAL(38, 38));
  assertReturnType(generator, signature, DECIMAL(38, 0));
  assertEmptyArgs(generator, signature, DECIMAL(18, 18));
}

TEST_F(ArgTypesGeneratorTest, multiply) {
  const auto& signature = getOnlySignature("multiply");
  const auto generator =
      std::make_shared<exec::test::MultiplyArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 2));
  assertReturnType(generator, signature, DECIMAL(18, 18));
  assertReturnType(generator, signature, DECIMAL(32, 6));
  assertReturnType(generator, signature, DECIMAL(38, 20));
  assertReturnType(generator, signature, DECIMAL(38, 38));
  assertReturnType(generator, signature, DECIMAL(38, 0));
}

TEST_F(ArgTypesGeneratorTest, divide) {
  const auto& signature = getOnlySignature("divide");
  const auto generator =
      std::make_shared<exec::test::DivideArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 2));
  assertReturnType(generator, signature, DECIMAL(18, 18));
  assertReturnType(generator, signature, DECIMAL(32, 6));
  assertReturnType(generator, signature, DECIMAL(38, 20));
  assertReturnType(generator, signature, DECIMAL(38, 38));
  assertReturnType(generator, signature, DECIMAL(38, 0));
}

TEST_F(ArgTypesGeneratorTest, floor) {
  const auto& signature = getOnlySignature("floor");
  const auto generator =
      std::make_shared<exec::test::FloorAndRoundArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 0));
  assertReturnType(generator, signature, DECIMAL(18, 0));
  assertReturnType(generator, signature, DECIMAL(32, 0));
  assertReturnType(generator, signature, DECIMAL(38, 0));
  assertEmptyArgs(generator, signature, DECIMAL(10, 2));
}

TEST_F(ArgTypesGeneratorTest, round) {
  const auto signatures = getSignatures("round", "decimal");
  POLLUX_CHECK_EQ(signatures.size(), 2);
  bool isSingleArg = signatures[0]->argumentTypes().size() == 1;
  const auto generator =
      std::make_shared<exec::test::FloorAndRoundArgTypesGenerator>();

  const auto& singleArgSignature =
      isSingleArg ? *signatures[0] : *signatures[1];
  assertReturnType(generator, singleArgSignature, DECIMAL(10, 0));
  assertReturnType(generator, singleArgSignature, DECIMAL(18, 0));
  assertReturnType(generator, singleArgSignature, DECIMAL(32, 0));
  assertReturnType(generator, singleArgSignature, DECIMAL(38, 0));
  assertEmptyArgs(generator, singleArgSignature, DECIMAL(10, 2));

  const auto& twoArgsSignature = isSingleArg ? *signatures[1] : *signatures[0];
  assertReturnType(generator, twoArgsSignature, DECIMAL(10, 2));
  assertReturnType(generator, twoArgsSignature, DECIMAL(32, 6));
  assertReturnType(generator, twoArgsSignature, DECIMAL(38, 20));
  assertReturnType(generator, twoArgsSignature, DECIMAL(38, 0));
  assertEmptyArgs(generator, twoArgsSignature, DECIMAL(18, 18));
  assertEmptyArgs(generator, twoArgsSignature, DECIMAL(38, 38));
  assertEmptyArgs(generator, twoArgsSignature, DECIMAL(1, 0));
}

TEST_F(ArgTypesGeneratorTest, modulus) {
  const auto& signature = getOnlySignature("mod");
  const auto generator =
      std::make_shared<exec::test::ModulusArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(10, 2));
  assertReturnType(generator, signature, DECIMAL(32, 6));
  assertReturnType(generator, signature, DECIMAL(38, 20));
  assertReturnType(generator, signature, DECIMAL(38, 38));
  assertReturnType(generator, signature, DECIMAL(38, 0));
}

TEST_F(ArgTypesGeneratorTest, truncate) {
  const auto signatures = getSignatures("truncate", "decimal");
  POLLUX_CHECK_EQ(signatures.size(), 2);
  const auto& signature = signatures[0]->argumentTypes().size() == 1
      ? *signatures[0]
      : *signatures[1];
  const auto generator =
      std::make_shared<exec::test::TruncateArgTypesGenerator>();

  assertReturnType(generator, signature, DECIMAL(1, 0));
  assertReturnType(generator, signature, DECIMAL(10, 0));
  assertReturnType(generator, signature, DECIMAL(18, 0));
  assertReturnType(generator, signature, DECIMAL(32, 0));
  assertReturnType(generator, signature, DECIMAL(38, 0));
  assertEmptyArgs(generator, signature, DECIMAL(10, 2));
}

} // namespace
